const app = getApp()
const request = require('../../utils/https.js')
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: ['全部', '待付款', '待发货', '待收货', '售后服务'],
    active: 0,
    visible4: false,
    visible5: false,
    visible6: false,
    visible7: false,
    actions4: [{
        name: '取消'
      },
      {
        name: '确定',
        color: '#ed3f14'
      }
    ],
    actions5: [{
        name: '取消'
      },
      {
        name: '删除',
        color: '#ed3f14'
      }
    ],
    actions6: [{
        name: '取消'
      },
      {
        name: '确定',
        color: '#ed3f14'
      }
    ],
    actions7: [{
        name: '取消'
      },
      {
        name: '确定',
        color: '#ed3f14'
      }
    ],
    loading: false,
    has: false,
    fixed: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options===>', options)


    userId = wx.getStorageSync("userId");
    if (options.msg) {
      this.setData({
        msg: options.msg
      })
    }
    const that = this;
    if (options.navId != undefined) {
      let type = options.navId; //0 1 2 3 4
      that.setData({
        active: type
      })
    }
    let navIndex = this.data.active;
    that.orderList(navIndex)

  },


  orderList(navIndex) {
    const that = this;
    request._get(getApp().Api_url + 'order/mine', { //订单列表接口
      userId: userId,
      kinds: navIndex,
      pageNum: 1,
      pageSize: 100
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var orderList = res.data.data;
        console.log('orderList==>', orderList)
        orderList.forEach((v) => {
          if (v.orderDiscount != 10) {
            v.orderDiscount = (v.totalMoney / v.billMoney).toFixed(2) * 10
          }
        });
        that.setData({
          orderList: orderList
        })
      } else {
        that.setData({
          orderList: []
        })
      }
    }, function () {})
  },

  onClick(event) {
    const that = this;
    let navIndex = event.detail.index;
    let navName = event.detail.title;
    let navList = that.data.navList;
    that.orderList(navIndex)
    that.setData({
      active: navIndex
    })

  },

  //查看售后详情
  goToAfterSales(e) {
    const orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '/pages/after_type/after_type?orderid=' + orderid,
    })
  },

  //申请售后
  goToApply(e) {
    const orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '/pages/after_sales/after_sales?orderid=' + orderid,
    })
  },
  //订单详情
  goToDetail(e) {
    let orderid = e.currentTarget.dataset.orderid;
    let orderStates = e.currentTarget.dataset.orderstates;
    //点击订单看详情
    if (orderStates != 4 || orderStates != 5 || orderStates != 6 || orderStates != 7 || orderStates != 8) {
      console.log('不等于45678')
      wx.navigateTo({
        url: '/pages/order1_details/order1_details?orderid=' + orderid
      })
    }
  },
  //确认收货
  orderOk(orderid, navIndex) {
    const that = this;
    request._get(getApp().Api_url + 'order/confirm', { //确认收货
      orderId: orderid
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        that.orderList(navIndex)
      } else {

      }
    }, function () {})
  },
  //查看物流
  goToCourier(e) {
    const orderid = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: '/pages/Courier_look/Courier_look?orderid=' + orderid,
    })
  },
  handleClose1(e) {
    console.log(e)
    const that = this;
    if (e.type == 'ok') {
      console.log('原价支付')
      that.orderPay(that.data.orderid);

    } else {
      console.log('取消支付')
      that.setData({
        visible1: false
      });
    }
  },
  handleClick4({
    detail
  }) {
    const that = this;
    if (detail.index === 0) {
      that.setData({
        visible4: false
      });
      console.log('取消')
    } else {
      console.log('确定')
      that.cancelFn(that.data.orderid, that.data.navIndex);
      that.setData({
        visible4: false
      });
    }
  },
  handleClick5({
    detail
  }) {
    const that = this;
    if (detail.index === 0) {
      that.setData({
        visible5: false
      });
      console.log('取消')
    } else {
      console.log('删除')
      that.deleteFn(that.data.orderid, that.data.navIndex);
      that.setData({
        visible5: false
      });
    }
  },
  handleClick6({
    detail
  }) {
    const that = this;
    if (detail.index === 0) {
      that.setData({
        visible6: false
      });
      console.log('取消')
    } else {
      console.log('确认支付')
      that.payFn(that.data.orderid, that.data.orderMoney);
      that.setData({
        visible6: false,
        loading: true
      });
    }
  },
  handleClick7({
    detail
  }) {
    const that = this;
    if (detail.index === 0) {
      that.setData({
        visible7: false
      });
      console.log('取消')
    } else {
      console.log('确认')
      that.orderOk(that.data.orderid, that.data.navIndex);
      that.setData({
        visible7: false,
        loading: false
      });
    }
  },
  payFn(orderid, orderMoney) {
    const that = this;
    request._get(getApp().Api_url + "order/payIt", {
      userId: userId,
      orderId: orderid,

    }, (res) => {
      that.setData({
        loading: false
      })
      let params = res.data.data;
      // 去微信支付
      wx.requestPayment({
        'appId': params.appId,
        'timeStamp': params.timeStamp + '',
        'nonceStr': params.nonceStr,
        'package': params.packageValue,
        'signType': 'MD5',
        'paySign': params.paySign,
        'success': (payres) => {
          console.log("payres", payres)

          if (payres.errMsg == 'requestPayment:ok') {
            /**
             * 支付成功
             */
            wx.redirectTo({
              url: '/pages/pay_success/pay_success?orderMoney=' + orderMoney,
            })
          }
        },
        'fail': (xhr) => {
          console.log("xhr", xhr)
          if (xhr.errMsg == 'requestPayment:fail cancel') {
            wx.reLaunch({
              url: '/pages/my_order/my_order?msg=cancel'
            })


          }
        }
      })

    }, (xhr) => {

    })
  },

  //付款
  goPay(e) {
    const that = this;
    const orderid = e.currentTarget.dataset.orderid;
    let orderMoney = e.currentTarget.dataset.ordermoney;
    that.setData({
      visible6: true,
      orderid: orderid,
      orderMoney: orderMoney
    })
  },


  //取消订单
  cancelOrder(e) {
    const that = this;
    const orderid = e.currentTarget.dataset.orderid;
    let navIndex = that.data.active;
    that.setData({
      visible4: true,
      orderid: orderid,
      navIndex: navIndex
    })
  },
  //确认收货
  postOk(e) {
    const that = this;
    const orderid = e.currentTarget.dataset.orderid;
    let navIndex = that.data.active;
    that.setData({
      visible7: true,
      orderid: orderid,
      navIndex: navIndex
    })
  },
  //删除订单
  deleteOrder(e) {
    const that = this;
    const orderid = e.currentTarget.dataset.orderid;
    let navIndex = that.data.active;
    that.setData({
      visible5: true,
      orderid: orderid,
      navIndex: navIndex
    })
  },
  cancelFn(orderid, navIndex) {
    const that = this;
    request._get(getApp().Api_url + 'order/cancelIt', {
      userId: userId,
      reason: ' ',
      orderId: orderid
    }, function (res) {
      console.log('res===>', res)
      if (res.data.errMsg == 'OK') {

        wx.showToast({
          icon: 'none',
          title: '取消成功'
        })

        that.orderList(navIndex)

      }
    }, function () {})
  },
  deleteFn(orderid, navIndex) {
    const that = this;
    request._get(getApp().Api_url + 'order/delete', {
      userId: userId,
      orderId: orderid
    }, function (res) {
      console.log('res===>', res)
      if (res.data.errMsg == 'OK') {
        wx.showToast({
          icon: 'none',
          title: '删除成功'
        })

        that.orderList(navIndex)

      }
    }, function () {})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    let navIndex = this.data.active;
    that.orderList(navIndex);
    that.upWish();
  },

  //返回商城
  goShopping() {
    wx.switchTab({
      url: '/pages/list/list'
    })
  },
  //升级后的效果
  //升级后的效果
  upWish() {
    const that = this;
    request._get(getApp().Api_url + 'rank/upWish', {
      userId: userId
    }, function (res) {
      console.log('升级后的效果===>', res.data.data)
      if (res.data.code == 200) {
        if (res.data.data.has) {
          that.setData({
            fixed: 'fixed;width:100%'
          })
        } else {
          that.setData({
            fixed: ''
          })
        }
        that.setData({
          wishImg: res.data.data.wishImg,
          has: res.data.data.has
        })
      }
    }, function () {})
  },
  postOrder() {
    const that = this;
    request._get(getApp().Api_url + 'rank/upRead', {
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          has: false,
          fixed: ''
        })
      }
    }, function () {})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    /* if (this.data.msg == "cancel") {//
      wx.switchTab({
        url: '/pages/list/list',
      })
    } */
    /* else {
         wx.switchTab({
           url: '/pages/my/my',
         })
       } */

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.msg == "cancel") {
      wx.switchTab({
        url: '/pages/list/list',
      })
    }
    /* else {
         wx.switchTab({
           url: '/pages/my/my',
         })
       } */

  },

})